package com.kust.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kust.commomUtils.Result;
import com.kust.eduService.entity.Course;
import com.kust.eduService.entity.vo.CourseInfoVo;
import com.kust.eduService.entity.vo.CoursePublishVo;
import com.kust.eduService.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-23
 */
@Api(tags = "课程控制器")
@RestController
@RequestMapping("/eduService/edu-course")
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    //课程列表 TODO
    // 条件查询带分页
    @ApiOperation(value = "分页课程列表")
    @GetMapping("getCourseList/{current}/{limit}")
    public Result getCourseList(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable("current") Long current,
                                @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable("limit") Long limit){

        //创建Page对象
        //调用方法时，底层封装，
        Page<Course> eduCoursePage= new Page<>(current,limit);
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc();
        courseService.page(eduCoursePage,queryWrapper);
        long total=eduCoursePage.getTotal(); //记录总页数
        List<Course> records = eduCoursePage.getRecords();//数据list集合
        return Result.ok().data("total",total).data("rows",records);
    }

    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public Result addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        //返回添加之后课程id，为了后面添加大纲使用
        String id = courseService.saveCourseInfo(courseInfoVo);
        return Result.ok().data("courseId",id);
    }

    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public Result getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return Result.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public Result updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        System.out.println("courseInfoVo===>"+courseInfoVo);
        courseService.updateCourseInfo(courseInfoVo);
        return Result.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("/getPublishCourseInfo/{id}")
    public Result getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return Result.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布
    //修改课程状态
    @PostMapping("publishCourse/{id}")
    public Result publishCourse(@PathVariable String id) {
        Course eduCourse = new Course();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态
        courseService.updateById(eduCourse);
        return Result.ok();
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public Result deleteCourse(@PathVariable String courseId) {
        courseService.removeCourse(courseId);
        return Result.ok();
    }


}

